package org.idea.qiyu.framework.rpc.dubbo.router.starter.config;

import org.apache.dubbo.config.ApplicationConfig;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;

import javax.annotation.Resource;
import java.util.HashMap;

/**
 * 和dubbo相关的配置类
 *
 * @Author linhao
 * @Date created in 11:16 下午 2021/8/2
 */
@Configuration
public class QiyuDubboConfig implements BeanPostProcessor {

    @Resource
    private Environment environment;

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        if (bean instanceof ApplicationConfig) {
            String zone = environment.getProperty("zone");
            System.out.println("[QiyuDubboConfig] 路由配置启动");
            ApplicationConfig applicationConfig = (ApplicationConfig) bean;
            if (applicationConfig.getParameters() == null) {
                applicationConfig.setParameters(new HashMap<>(1));
            }
            applicationConfig.getParameters().putIfAbsent("zone", zone);
        }
        return bean;
    }
}
